<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"baweifan.gitee.io","root":"/myblog/","images":"/myblog/images","scheme":"Pisces","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>
<meta name="description" content="1、BASE_DIR这里是 Django提供的 Django项目根目录–绝对路径 E:\workspace2021\shixun01\resp_2005A\WorkerOrder 如果我们在项目中要直接引入某包、文件则一定要保证其上级路径是在sys.path中。 这里我们把app都创建到了apps目录中，则需要把apps也添加到sys.path中 sys.path是一个查找路径列表，把apps路径">
<meta property="og:type" content="article">
<meta property="og:title" content="Django复习">
<meta property="og:url" content="http://baweifan.gitee.io/myblog/2021/04/03/day02/index.html">
<meta property="og:site_name" content="IT教育">
<meta property="og:description" content="1、BASE_DIR这里是 Django提供的 Django项目根目录–绝对路径 E:\workspace2021\shixun01\resp_2005A\WorkerOrder 如果我们在项目中要直接引入某包、文件则一定要保证其上级路径是在sys.path中。 这里我们把app都创建到了apps目录中，则需要把apps也添加到sys.path中 sys.path是一个查找路径列表，把apps路径">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-04-03T13:13:28.000Z">
<meta property="article:modified_time" content="2021-04-03T14:00:21.416Z">
<meta property="article:author" content="大海老师">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://baweifan.gitee.io/myblog/2021/04/03/day02/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>Django复习 | IT教育</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">IT教育</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1%E3%80%81BASE-DIR"><span class="nav-number">1.</span> <span class="nav-text">1、BASE_DIR</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1%E3%80%81RESTFul%E6%8E%A5%E5%8F%A3%E9%A3%8E%E6%A0%BC"><span class="nav-number">2.</span> <span class="nav-text">1、RESTFul接口风格</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2%E3%80%81Mixin%E7%B1%BB"><span class="nav-number">3.</span> <span class="nav-text">2、Mixin类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3%E3%80%81GenericAPIView%E7%B1%BB"><span class="nav-number">4.</span> <span class="nav-text">3、GenericAPIView类</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="大海老师"
      src="/myblog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">大海老师</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">3</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/yourname" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/yourname" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://baweifan.gitee.io/myblog/2021/04/03/day02/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/myblog/images/avatar.jpg">
      <meta itemprop="name" content="大海老师">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="IT教育">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Django复习
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-04-03 21:13:28 / 修改时间：22:00:21" itemprop="dateCreated datePublished" datetime="2021-04-03T21:13:28+08:00">2021-04-03</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="1、BASE-DIR"><a href="#1、BASE-DIR" class="headerlink" title="1、BASE_DIR"></a>1、BASE_DIR</h2><p>这里是 Django提供的 Django项目根目录–绝对路径</p>
<p>E:\workspace2021\shixun01\resp_2005A\WorkerOrder</p>
<p>如果我们在项目中要直接引入某包、文件则一定要保证其上级路径是在sys.path中。</p>
<p>这里我们把app都创建到了apps目录中，则需要把apps也添加到sys.path中</p>
<p>sys.path是一个查找路径列表，把apps路径添加到列表的第一个位置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sys.path.insert(0, os.path.join(BASE_DIR, &quot;apps&quot;))</span><br></pre></td></tr></table></figure>





<p>2、配置django项目启动服务</p>
<h2 id="1、RESTFul接口风格"><a href="#1、RESTFul接口风格" class="headerlink" title="1、RESTFul接口风格"></a>1、RESTFul接口风格</h2><p>早期设计是以功能为核心设计接口</p>
<p>/addBook   添加图书接口</p>
<p>/findBook  查询图书接口</p>
<p>/deleteBook 删除接口</p>
<p>/updateBook 更新接口</p>
<p>2000年的时候，Rest概念:表现层状态转移    </p>
<p>以资源为核心设计路径，以HTTP动词来做资源的状态改变</p>
<p>以RESTful设计接口</p>
<p>/books     图书资源接口</p>
<p>​    GET</p>
<p>​    POST</p>
<p>​    DELETE</p>
<p>​    PUT</p>
<p>查询资源列表  、添加资源</p>
<p>查询单个、修改、删除   都需要资源的ID</p>
<h2 id="2、Mixin类"><a href="#2、Mixin类" class="headerlink" title="2、Mixin类"></a>2、Mixin类</h2><p>1、RetrieveModelMixin类</p>
<p>需要pk，提供了查询单个的方法 retrieve</p>
<p>2、ListModelMixin类</p>
<p>提供了查询列表的方法 list</p>
<p>3、CreateModelMixin类</p>
<p>提供了创建的方法 create</p>
<p>4、UpdateModelMixin类</p>
<p>需要pk，提供了更新的方法update  和部分更新partial_update</p>
<p>5、DestroyModelMixin类</p>
<p>需要pk，提供了删除的方法destory</p>
<h2 id="3、GenericAPIView类"><a href="#3、GenericAPIView类" class="headerlink" title="3、GenericAPIView类"></a>3、GenericAPIView类</h2><p>该类是APIView的子类，封装更严谨</p>
<p>1、常用属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">serializer_class &#x3D; TypeSer</span><br><span class="line"># 相应的查询集</span><br><span class="line">queryset &#x3D; Type.objects.all()</span><br></pre></td></tr></table></figure>

<p>2、常用方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">def get_queryset(self):</span><br><span class="line">    pass</span><br><span class="line"></span><br><span class="line">def get_serializer_class(self):</span><br><span class="line">    pass</span><br></pre></td></tr></table></figure>

<p>当serializer_class 或queryset 属性 满足不了要求的时候，可以使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">get_queryset方法替代 queryset 属性 提供值</span><br><span class="line">get_serializer_class方法替代 serializer_class 属性 提供值</span><br></pre></td></tr></table></figure>

<p>​       </p>

    </div>

    
    
    

    <footer class="post-footer">

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/myblog/2021/04/03/2006-day01/" rel="prev" title="Linux常用命令">
                  <i class="fa fa-chevron-left"></i> Linux常用命令
                </a>
            </div>
            <div class="post-nav-item">
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">大海老师</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/myblog/js/utils.js"></script><script src="/myblog/js/motion.js"></script><script src="/myblog/js/next-boot.js"></script>

  






  





</body>
</html>
